﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace draw
{
    public partial class Form1 : Form
    {
        //抽取的名单
        static string[] students ={"Tom","David","Mary","Wilson",
                                   "Jack","Peter","Tina","Jane",
                                   "Siiri","Via","Cyril","Fitz","Caesar" };

        //名单长度
        static string _lenth = students.Length.ToString();
        static int lenth = int.Parse(_lenth);

        //抽取人数
        int num = 1;

        //生成随机数的函数
        int randint(int a,int b)
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();//生成字节数组
            int iRoot = BitConverter.ToInt32(buffer, 0);//利用BitConvert方法把字节数组转换为整数
            Random rdmNum = new Random(iRoot);//以这个生成的整数为种子
            int ans = rdmNum.Next(a, b);//生成大于等于a小于b的随机数（int）型
            return ans;
        }

        //用于打乱名单的顺序
        void upset()
        {
            for(int i=0;i<lenth;i++)
            {
                int tmp = randint(0, lenth);
                string temp = students[i];
                students[i] = students[tmp];
                students[tmp] = temp;

            }
        }

        //用于判断名单是否已遍历完毕，当i=0时说明列表以遍历完毕需要重新打乱
        int position = 0;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置comboBox的默认值为1
            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("1");            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //当用户改变了抽取人数后改变那num值
            num = int.Parse(comboBox1.Text);

            //将label的颜色进行相应的改变
            switch(num)
            {
                case 1:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(0, 0, 0);
                    name3.ForeColor = Color.FromArgb(0, 0, 0);
                    name4.ForeColor = Color.FromArgb(0, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(0, 0, 0);
                    name7.ForeColor = Color.FromArgb(0, 0, 0);
                    name8.ForeColor = Color.FromArgb(0, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 2:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(0, 0, 0);
                    name3.ForeColor = Color.FromArgb(0, 0, 0);
                    name4.ForeColor = Color.FromArgb(0, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(0, 0, 0);
                    name8.ForeColor = Color.FromArgb(0, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 3:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(0, 0, 0);
                    name3.ForeColor = Color.FromArgb(0, 0, 0);
                    name4.ForeColor = Color.FromArgb(0, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(0, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 4:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(0, 0, 0);
                    name3.ForeColor = Color.FromArgb(0, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(0, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 5:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(0, 0, 0);
                    name3.ForeColor = Color.FromArgb(255, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(0, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 6:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(0, 0, 0);
                    name3.ForeColor = Color.FromArgb(255, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(255, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 7:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(255, 0, 0);
                    name3.ForeColor = Color.FromArgb(255, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(255, 0, 0);
                    name9.ForeColor = Color.FromArgb(0, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 8:
                    name1.ForeColor = Color.FromArgb(0, 0, 0);
                    name2.ForeColor = Color.FromArgb(255, 0, 0);
                    name3.ForeColor = Color.FromArgb(255, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(255, 0, 0);
                    name9.ForeColor = Color.FromArgb(255, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                case 9:
                    name1.ForeColor = Color.FromArgb(255, 0, 0);
                    name2.ForeColor = Color.FromArgb(255, 0, 0);
                    name3.ForeColor = Color.FromArgb(255, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(255, 0, 0);
                    name9.ForeColor = Color.FromArgb(255, 0, 0);
                    name10.ForeColor = Color.FromArgb(0, 0, 0);
                    break;

                default:
                    name1.ForeColor = Color.FromArgb(255, 0, 0);
                    name2.ForeColor = Color.FromArgb(255, 0, 0);
                    name3.ForeColor = Color.FromArgb(255, 0, 0);
                    name4.ForeColor = Color.FromArgb(255, 0, 0);
                    name5.ForeColor = Color.FromArgb(255, 0, 0);
                    name6.ForeColor = Color.FromArgb(255, 0, 0);
                    name7.ForeColor = Color.FromArgb(255, 0, 0);
                    name8.ForeColor = Color.FromArgb(255, 0, 0);
                    name9.ForeColor = Color.FromArgb(255, 0, 0);
                    name10.ForeColor = Color.FromArgb(255, 0, 0);
                    break;
            }
        }



        private void timer1_Tick_1(object sender, EventArgs e)
        {
            //当列表遍历完毕后重新打乱列表
            if(position==0)
            {
                upset();
                position = lenth-1;
            }
            string temp = name1.Text, tmp;
            name1.Text = students[position];
            tmp = name2.Text;
            name2.Text = temp;
            temp = name3.Text;
            name3.Text = tmp;
            tmp = name4.Text;
            name4.Text = temp;
            temp = name5.Text;
            name5.Text = tmp;
            tmp = name6.Text;
            name6.Text = temp;
            temp = name7.Text;
            name7.Text = tmp;
            tmp = name8.Text;
            name8.Text = temp;
            temp = name9.Text;
            name9.Text = tmp;
            name10.Text = temp;
            position--;
        }

        private void button_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            if (timer1.Enabled)
            {
                button.Text = "停！";
            }
            else
            {
                button.Text = "开始抽奖";
            }
        }

        private void title1_Click(object sender, EventArgs e)
        {

        }
    }
}
